<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="80%"
    top="5%"
  >
    <div style="height: 65vh">
      <t-form ref="form" :data="formData" :colon="true" labelWidth="100px" :rules="rules">
        <t-form-item label="系统名称" name="name">
          <t-input clearable v-model="formData.name" placeholder="请输入系统名称"></t-input>
        </t-form-item>
        <t-form-item label="备注" name="note">
          <t-textarea v-model="formData.note" :autosize="{minRows: 3}"/>
        </t-form-item>
        <t-form-item label="系统状态" name="systemStatus">
          <t-radio-group :options="dict.type.system_status" v-model="formData.systemStatus"></t-radio-group>
        </t-form-item>
        <t-form-item label="完成时间" name="completeDate">
          <t-date-picker mode="month" v-model="formData.completeDate"></t-date-picker>
        </t-form-item>
      </t-form>
    </div>
  </t-dialog>
</template>

<script>
export default {
  name: "add-edit-system",
  props:{
    header:{
      type: String,
      default: '',
    }
  },
  data(){
    return {
      visible: true,
      formData:{
        name: '',
        note: ''
      },
      loading: false,
      rules:{
        opinion: [
          { required: true, message: "终止意见不能为空" }
        ],
      }
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj();
        }
      }
    },
  },
  methods:{
    getObj() {
      this.$api.system.monitor.jobGet(this.objId).then(response => {
        Object.assign(this.formData, response.data)
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if (this.objId) {
            this.$api.system.monitor.jobUpdate(this.formData).then(() => {
              this.$message.success("修改成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          } else {
            this.$api.system.monitor.jobAdd(this.formData).then((response) => {
              this.$message.success("新增成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }
        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      });
    },
  }
}
</script>

<style scoped>

</style>
